package cn.forkway.learn.jdbcTry.utils;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * @program: java-learn-jdbc
 * @className: DBUtil.java
 * @author: River Ray
 * @description: JDBC封装
 * @createTime 2021年10月08日 19:52:00
 */
public class DBUtil {
    static String URL;
    static String DRIVER;
    static String USER;
    static String PWD;

    static {
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        DRIVER = bundle.getString("driver");
        URL = bundle.getString("url");
        USER = bundle.getString("user");
        PWD = bundle.getString("pwd");
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private DBUtil() {
    }

    ;


    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PWD);
    }

    public static void close(Connection conn, Statement ps, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
